package com.jctrip.hotel.source.xxl.greentree.thread;

import com.jctrip.hotel.common.constant.RedisKeyConstant;
import com.jctrip.hotel.common.context.BeanContext;
import com.jctrip.hotel.common.util.RedisUtil;
import com.jctrip.hotel.source.service.greentree.GreenTreeService;
import com.jctrip.hotel.source.service.greentree.GreenTreeTaskService;
import com.jctrip.hotel.source.service.hworld.HworldTaskService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @Author：Haruhi
 * @description:
 * @Date：2023/9/5 10:36
 * @Package：com.jctrip.hotel.source.xxl.greentree.thread
 * @Project：hotel-main
 */

@Slf4j
public class SyncGreenTreeRatesTaskThread implements Runnable{
    private final String chainCode;
    private final Integer days;
    private final String level;
    private final String sourceId;

    public SyncGreenTreeRatesTaskThread(String chainCode, Integer days, String level, String sourceId) {
        this.chainCode = chainCode;
        this.days = days;
        this.level = level;
        this.sourceId = sourceId;
    }

    @Override
    public void run() {
        RedisUtil redisUtil = BeanContext.getApplicationContext().getBean(RedisUtil.class);
        GreenTreeTaskService greenTreeTaskService = BeanContext.getApplicationContext().getBean(GreenTreeTaskService.class);
        GreenTreeService greenTreeService = BeanContext.getApplicationContext().getBean(GreenTreeService.class);

        while (redisUtil.lGetListSize(RedisKeyConstant.HOTEL_MAIN_GREENTREE_RATES_LIST) > 0) {
            try {
                String hotelId = (String) redisUtil.rightPop(RedisKeyConstant.HOTEL_MAIN_GREENTREE_RATES_LIST);
                if (StringUtils.isBlank(hotelId)) {
                    continue;
                }
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                Date start = new Date();
                Date end = DateUtils.addDays(start,days);
                greenTreeTaskService.syncGreenTreeRatesTask(chainCode,greenTreeService.parseHotelId(hotelId),
                        simpleDateFormat.format(start),simpleDateFormat.format(end), level, sourceId);
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            }
        }

    }
}
